package com.example.dzbsh.service;

import com.example.dzbsh.dao.FujianMapper;
import com.example.dzbsh.model.Fujian;
import com.example.dzbsh.model.FujianExample;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class FujianService {

    @Resource
    FujianMapper ddMapper;
    @Value("./userfiles")
    String userfilepath;

    public boolean add(Fujian ppFujian) {
        return ddMapper.insert(ppFujian)==1;
    }

      public boolean delete(String ppId) {
        return ddMapper.deleteByPrimaryKey(ppId)==1;
    }

     /*public boolean deleteByGuifanID(String ppGuifanId) {
        return ddMapper.deleteByGuifanID(ppGuifanId)==1;
    }*/

    public Fujian selectByPrimaryKey(String ppId) {
        return ddMapper.selectByPrimaryKey(ppId);
    }

    public List<Fujian> findByGuifanluoshiidAndZhengmingcailiaoId(String ppGuifanluoshiId,String ppZhengmingcailiaoId) {
        FujianExample mmExample = new FujianExample();
        mmExample.createCriteria().andGuifanluoshiidEqualTo(ppGuifanluoshiId).andZhengmingcailiaoidEqualTo(ppZhengmingcailiaoId);
        return ddMapper.selectByExample(mmExample);
    }

    /*public Integer countByGuifanluoshiidAndZhengmingcailiaoId(String ppGuifanluoshiId,String ppZhengmingcailiaoId) {
        Object mmCount = ddMapper.countByGuifanluoshiidAndZhengmingcailiaoId(ppGuifanluoshiId,ppZhengmingcailiaoId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }*/

    /*
     * 判断指定的文件或文件夹删除是否成功
     * @param FileName 文件或文件夹的路径
     * @return true or false 成功返回true，失败返回false
     */
    public boolean deleteAnyone(String FileName){

        FileName = userfilepath+FileName;
        File file = new File(FileName);//根据指定的文件名创建File对象


        if ( !file.exists() ){
            return false;
        }else{ //要删除的文件存在

            if ( file.isFile() ){ //如果目标文件是文件

                return deleteFile(FileName);

            }/*else{  //如果目标文件是目录
                return deleteDir(FileName);
            }*/
            return true;
        }
    }




    /*
     * 判断指定的文件删除是否成功
     */
    public static boolean deleteFile(String fileName){


        File file = new File(fileName);//根据指定的文件名创建File对象

        if (  file.exists() && file.isFile() ){ //要删除的文件存在且是文件

            if ( file.delete()){
                System.out.println("文件"+fileName+"删除成功！");
                return true;
            }else{
                System.out.println("文件"+fileName+"删除失败！");
                return false;
            }
        }else{

            System.out.println("文件"+fileName+"不存在，删除失败！" );
            return false;
        }

    }

    /*
     * 删除指定的目录以及目录下的所有子文件
     * @param dirName is 目录路径
     * @return true or false 成功返回true，失败返回false
     */
    public static boolean deleteDir(String dirName){

        if ( dirName.endsWith(File.separator) )//dirName不以分隔符结尾则自动添加分隔符
            dirName = dirName + File.separator;

        File file = new File(dirName);//根据指定的文件名创建File对象

        if ( !file.exists() || ( !file.isDirectory() ) ){ //目录不存在或者
            System.out.println("目录删除失败"+dirName+"目录不存在！" );
            return false;
        }

         file.delete();//删除当前目录
            System.out.println("目录"+dirName+"删除成功！" );


        return true;

    }

    public int deleteByGuifanId(String ppGuifanId){
        FujianExample mmExample = new FujianExample();
        FujianExample.Criteria mmCriteria = mmExample.createCriteria();
        mmCriteria.andGuifanluoshiidEqualTo(ppGuifanId);
        return ddMapper.deleteByExample(mmExample);
    }

    @Transactional
    public boolean deleteCustom(String ppId) {

        Fujian mmFujian = selectByPrimaryKey(ppId);
        if (mmFujian==null){
            throw new RuntimeException("不存在该附件");
        }

        String mmNewFileName = mmFujian.getNewfilename() == null?"":mmFujian.getNewfilename();
        Boolean mmdeleteByIdResult = delete(ppId);
        Boolean mmdeleteWenjian = deleteAnyone(mmNewFileName);
        if (!mmdeleteByIdResult){
            throw new RuntimeException("删除附件数据失败");
        }
        if (!mmdeleteWenjian){
            throw new RuntimeException("删除文件失败");
        }
        return true;
    }
}
